package interrupt;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author fei
 * @date 2023/1/10 16:24
 * @description Object 使用 wait 和 notify 方法
 */
public class LockSupportDemo {

    public static void main(String[] args) {
        // sleep 方法 3 秒后醒来，执行 park 无效，没有阻塞效果
        // 类似高速公路的 ETC，提前买好了通行证 unpark，到闸机处直接抬起栏杆放行了，没有 park 拦截了
        Thread t1 = new Thread(() -> {
            try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println(Thread.currentThread().getName() + "\t ----- come in " + System.currentTimeMillis());
            LockSupport.park();
            System.out.println(234234234);
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "\t ----- 被唤醒 " + System.currentTimeMillis());
        }, "t1");
        t1.start();

        // try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }

        new Thread(() -> {
            LockSupport.unpark(t1);
            // 上限只有一个通行证，用掉一次就消耗掉了，所以下面调用也就没用了
            LockSupport.unpark(t1);
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "\t ----- 发出通知");
        }, "t2").start();
    }

    /**
     * LockSupport 正常用法
     */
    private static void m4() {
        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t ----- come in");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "\t ----- 被唤醒");
        }, "t1");
        t1.start();

        try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }

        new Thread(() -> {
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "\t ----- 发出通知");
        }, "t2").start();
    }

    /**
     * 先 await() 后 signal 才 Ok，否则线程无法被唤醒
     */
    private static void lockAwaitSignal() {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(() -> {
            try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName() + "\t ---- come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t ---- come in");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1").start();

        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t ----- 发出通知");
            } finally {
                lock.unlock();
            }
        }, "t2").start();
    }

    /**
     * 在 lock、unlock 对里面才能正确调用 condition 中线程等待和唤醒的方法
     */
    private static void m3() {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(() -> {
            // lock.lock();
            try{
                System.out.println(Thread.currentThread().getName() + "\t ---- come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t ---- come in");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // lock.unlock();
            }
        }, "t1").start();

        try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }

        new Thread(() -> {
            // lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t ----- 发出通知");
            } finally {
                // lock.unlock();
            }
        }, "t2").start();
    }

    /**
     * Condition 正常使用
     */
    private static void m2() {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(() -> {
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName() + "\t ---- come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t ---- come in");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1").start();

        try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }

        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t ----- 发出通知");
            } finally {
                lock.unlock();
            }
        }, "t2").start();
    }

    /**
     * 先 notify 后 wait，程序无法执行，唤醒
     */
    private static void syncWaitNotify() {
        Object objectLock = new Object();

        new Thread(() -> {
            try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + "\t -----come in");
                try {
                    objectLock.wait();
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
            }
        }, "t1").start();

        new Thread(() -> {
            synchronized (objectLock){
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + "\t -----发出通知");
            }
        }, "t2").start();
    }

    /**
     * 正常
     */
    private static void m1() {
        Object objectLock = new Object();
        new Thread(() -> {
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + "\t -----come in");
                try {
                    objectLock.wait();
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
            }
        }, "t1").start();

        try{ TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }

        new Thread(() -> {
            synchronized (objectLock){
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + "\t -----发出通知");
            }
        }, "t2").start();
    }

}
